package com.woniuxy.a_hello;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

/**
 * @author lg
 * @time 2020/07/04 10:20:49
 */
public class App {
    public static void main(String[] args) {
         // 创建安全管理器
        DefaultSecurityManager securityManager = new DefaultSecurityManager ();
         //  加载配置 也就是说，shiro配置文件中的信息 ，iniRealm
        IniRealm iniRealm = new IniRealm ("classpath:a/shiro.ini");
        //  此时securityManager 也知道 所有的用户信息
        securityManager.setRealm (iniRealm);
        // 把securityManager z设置成全局的 这一步是必须的
        SecurityUtils.setSecurityManager (securityManager);
         //进行认证
        UsernamePasswordToken token=new UsernamePasswordToken ("bar","456");
         // 获取主题 ，主体就是页当前系统进行交互的东东
        Subject subject = SecurityUtils.getSubject ();

        try {
            subject.login (token);
            System.out.println ("认证成功");
        } catch (AuthenticationException e) {
            System.out.println ("认证失败"+e.getMessage ());
            e.printStackTrace ();
        }

        System.out.println ("是否通过认证："+subject.isAuthenticated ());

         //授权  Permitted
        System.out.println("car:save " + subject.isPermitted("car:save"));
        System.out.println("car:delete " + subject.isPermitted("car:delete"));
        System.out.println("car:update " + subject.isPermitted("car:update"));
        System.out.println("car:find " + subject.isPermitted("car:find"));

        subject.logout ();
    }
}
